import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["tenantId", "unitCompanyId", "unitName", "unitCode", "expandFlag", "expandedRowKeys"];

/**
 * model 部门分配员工
 * @date: 2018-6-20
 */
import { getResponse } from 'utils/utils';
import { renderTreeData, search, searchAll, saveAdd, saveEdit, forbindLine, enabledLine as _enabledLine, gainCodeAndName } from "../services/departmentService";
export default {
  namespace: 'department',
  state: {
    pathMap: {},
    renderTree: [],
    addData: {},
    companyCode: '',
    companyName: '',
    tenantId: '',
    expandedRowKeys: []
  },
  effects: {
    // 获取部门信息
    fetchDepartmentInfo: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDepartmentInfo(_ref, _ref2) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchDepartmentInfo$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(gainCodeAndName, payload);

            case 5:
              _context.t1 = _context.sent;
              result = (0, _context.t0)(_context.t1);

              if (!result) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  companyCode: result.unitCode,
                  companyName: result.unitName
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchDepartmentInfo);
    }),
    // 获取部门数据列表(Tree)
    searchDepartmentData: /*#__PURE__*/_regeneratorRuntime.mark(function searchDepartmentData(_ref3, _ref4) {
      var payload, call, put, tenantId, unitCompanyId, unitName, unitCode, expandFlag, expandedRowKeys, others, result, expandedRow, _renderTreeData, renderTree, _renderTreeData$pathM, pathMap, unitIdList;

      return _regeneratorRuntime.wrap(function searchDepartmentData$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              tenantId = payload.tenantId, unitCompanyId = payload.unitCompanyId, unitName = payload.unitName, unitCode = payload.unitCode, expandFlag = payload.expandFlag, expandedRowKeys = payload.expandedRowKeys, others = _objectWithoutProperties(payload, _excluded);
              result = {};
              expandedRow = {};

              if (!(unitCode || unitName)) {
                _context2.next = 11;
                break;
              }

              _context2.next = 8;
              return call(search, {
                tenantId: tenantId,
                unitCompanyId: unitCompanyId,
                unitName: unitName,
                unitCode: unitCode
              });

            case 8:
              result = _context2.sent;
              _context2.next = 14;
              break;

            case 11:
              _context2.next = 13;
              return call(searchAll, {
                tenantId: tenantId,
                unitCompanyId: unitCompanyId
              });

            case 13:
              result = _context2.sent;

            case 14:
              result = getResponse(result);
              _renderTreeData = renderTreeData(result, {}), renderTree = _renderTreeData.renderTree, _renderTreeData$pathM = _renderTreeData.pathMap, pathMap = _renderTreeData$pathM === void 0 ? {} : _renderTreeData$pathM;

              if (!expandFlag) {
                expandedRow = {
                  expandedRowKeys: Object.keys(pathMap).map(function (item) {
                    return +item;
                  })
                };
              } else {
                unitIdList = result.map(function (item) {
                  return item.unitId;
                }) || [];
                expandedRow = {
                  expandedRowKeys: Array.from(new Set([].concat(_toConsumableArray(expandedRowKeys), _toConsumableArray(unitIdList))))
                };
              }

              _context2.next = 19;
              return put({
                type: 'updateState',
                payload: _objectSpread(_objectSpread({
                  renderTree: renderTree,
                  pathMap: pathMap
                }, expandedRow), others)
              });

            case 19:
            case "end":
              return _context2.stop();
          }
        }
      }, searchDepartmentData);
    }),
    // 添加部门信息(批量)
    saveAddData: /*#__PURE__*/_regeneratorRuntime.mark(function saveAddData(_ref5, _ref6) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveAddData$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(saveAdd, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveAddData);
    }),
    // 更新部门信息(单条)
    saveEditData: /*#__PURE__*/_regeneratorRuntime.mark(function saveEditData(_ref7, _ref8) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveEditData$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(saveEdit, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, saveEditData);
    }),
    // 禁用部门信息
    forbidLine: /*#__PURE__*/_regeneratorRuntime.mark(function forbidLine(_ref9, _ref10) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function forbidLine$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(forbindLine, payload);

            case 4:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, forbidLine);
    }),
    // 启用部门信息
    enabledLine: /*#__PURE__*/_regeneratorRuntime.mark(function enabledLine(_ref11, _ref12) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function enabledLine$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(_enabledLine, payload);

            case 4:
              result = _context6.sent;
              return _context6.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, enabledLine);
    })
  },
  reducers: {
    // 更新state
    updateState: function updateState(state, _ref13) {
      var payload = _ref13.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};